package com.blueocean_health.care.management.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.Building;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.BuildDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.mapper.BuildFloorDeptMapper;
import com.blueocean_health.care.management.mapper.BuildingMapper;
import com.blueocean_health.care.management.mapper.FloorMapper;
import com.blueocean_health.care.management.service.BuildService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 楼号管理 2018年1月4日20:29:26
 * @author wuyanli
 *
 */
@Service
public class BuildServiceImpl implements BuildService{
	
	@Resource
	private BuildingMapper buildDao;
	
	@Resource
	private FloorMapper floorDao;
	@Resource
	private BuildFloorDeptMapper buildFloorDao;

	/**
	 * 根据筛选条件查询楼号信息  2018年1月4日20:33:31
	 */
	@Override
	public PageInfo<BuildDto> getBulidList(Map<String, String> map) {
		String buildName = map.get("buildingName");
		if(!StringUtils.isEmpty(buildName)){
			buildName = buildName.trim();
			map.put("buildingName", buildName);
		}
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		int pageSize = Integer.parseInt(map.get("pageSize"));
		int page = Integer.parseInt(map.get("page"));
		PageHelper.startPage(page,pageSize);
		List<BuildDto> bulidList = buildDao.getBulidList(map);
		PageInfo<BuildDto> pageInfo = new PageInfo<BuildDto>(bulidList);
		return pageInfo;
	}
	/**
	 * 根据筛选条件导出楼号信息  2018年1月4日20:33:31
	 */
	@Override
	public List<BuildDto> bulidListExcel(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		List<BuildDto> bulidList = buildDao.getBulidList(map);
		return bulidList;
	}

	/**
	 * 根据Id 获取楼号信息   2018年1月5日09:45:49
	 */
	@Override
	public BuildDto getBulidById(Map<String, String> map) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
		BuildDto bulidById = buildDao.getBulidById(map);
		return bulidById;
	}

	/**
	 * 保存楼号信息 2018年1月5日10:02:32
	 */
	@Override
	public int insertBulid(Building build) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		build.setHospitalId(user.getHospitalId());
		build.setBuildingName(build.getBuildingName().trim());
		Map<String,String> map = new HashMap<>();
		map.put("hospitalId", user.getHospitalId());
		List<BuildDto> bulidNameList = getBulidNameList(map);
		for (BuildDto buildDto : bulidNameList) {
			if(build.getBuildingName().equals(buildDto.getBuildingName())){
				return -1;
			}
		}
		int insertBulid = buildDao.insertBulid(build);
		return insertBulid;
	}

	/**
	 * 修改楼号信息  2018年1月5日10:14:22
	 */
	@Override
	public int updateBuild(Building build) {
		//判断当前楼号是否已存在
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		Map<String,String> map = new HashMap<>();
		map.put("hospitalId", user.getHospitalId());
		map.put("buildingId", build.getBuildingId());
		List<BuildDto> bulidNameList = getBulidNameList(map);
		for (BuildDto buildDto : bulidNameList) {
			if(build.getBuildingName().equals(buildDto.getBuildingName())){
				return -1;
			}
		}
		build.setBuildingName(build.getBuildingName().trim());
		int updateBuild = buildDao.updateBuild(build);
		return updateBuild;
	}

	/**
	 * 根据Id进行逻辑删除  2018年1月5日10:15:19
	 */
	@Override
	public SysResult deleteBuild(String id) {
		
		List<String> idByBuildId = buildFloorDao.getIdByBuildId(id);
		if(idByBuildId.size() > 0){
			return SysResult.error(-1, "该楼号已被科室关联,无法删除");
		}
		buildDao.deleteBuild(id);
		return SysResult.ok(0, "删除成功");
	}

	/**
	 * 查询当前Id不在的其他楼号信息  2018年1月5日10:57:00
	 */
	@Override
	public List<BuildDto> getBulidNameList(Map<String, String> map) {
		List<BuildDto> bulidNameList = buildDao.getBulidNameList(map);
		return bulidNameList;
	}

	/**
	 * 楼号下拉  2018年1月5日16:24:52
	 */
	@Override
	public List<PullDownDto> getBulidPull() {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		List<PullDownDto> bulidPull = buildDao.getBulidPull(user.getHospitalId());
		return bulidPull;
	}

	/**
	 * 楼层下拉  2018年1月6日17:19:01
	 */
	@Override
	public List<PullDownDto> getFloorPull() {
		List<PullDownDto> floorPull = floorDao.getFloorPull();
		return floorPull;
	}

}
